package com.ceshiren.mini.service.impl;

import com.ceshiren.mini.converter.TestCaseConverter;
import com.ceshiren.mini.dao.TestcaseMapper;
import com.ceshiren.mini.dto.TestCaseDTO;
import com.ceshiren.mini.entity.Testcase;
import com.ceshiren.mini.service.TestCaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TestCaseServiceImpl implements TestCaseService {
    private TestCaseConverter testCaseConverter;

    @Autowired
    public void setTestCaseConverter(TestCaseConverter testCaseConverter) {
        this.testCaseConverter = testCaseConverter;
    }

    private TestcaseMapper TestcaseMapper;

    @Autowired
    public void setTestcaseMapper(TestcaseMapper TestcaseMapper) {
        this.TestcaseMapper = TestcaseMapper;
    }

    //往mysql数据库内添加用例
    @Override
    public int createTestCase(TestCaseDTO testCaseDTO) {
        int i = 0;

        //创建之前进行查看，当前测试用例是否存在，如果不存在则创建，如果存在则不创建
        Testcase testCase = new Testcase();
        testCase.setName(testCaseDTO.getName());
        testCase.setStatus(1);
        Testcase findByName = TestcaseMapper.selectOne(testCase);

        //测试用例不存在则创建
        if(null == findByName){
            System.out.println("可以进行用例添加");
            //1.testCaseDTO转成testCase
            Testcase testCase2 = testCaseConverter.testCaseDTOForTestCase(testCaseDTO);
            //3.testCase 创建时间、更新时间
            testCase2.setCreateTime(new Date());
            testCase2.setUpdateTime(new Date());
            //1 代表当前用例为可用
            testCase2.setStatus(1);
            //4.往数据库内添加对应值
            i = TestcaseMapper.insertUseGeneratedKeys(testCase2);
        }else {
            System.out.println("数据库已经存在该用例");
        }
        return i;
    }

    @Override
    public TestCaseDTO getTestCase(TestCaseDTO testCaseDTO) {
        //1. testCaseDTO转换为testCase
        Testcase testCase = testCaseConverter.testCaseDTOForTestCase(testCaseDTO);
        testCase.setStatus(1);
        //2. Mapper查找具体的testCase
        testCase = TestcaseMapper.selectOne(testCase);

        //3. testCase转换为testCaseDTO
        testCaseDTO = testCaseConverter.testCaseForTestCaseDTO(testCase);

        return testCaseDTO;
    }
    //查找所有的测试用例
    @Override
    public List<TestCaseDTO> getTestCaseList() {
        // status 不为1 则不显示
        Testcase testCase = new Testcase();
        testCase.setStatus(1);
        List<Testcase> testCaseList = TestcaseMapper.select(testCase);
//        List<TestCase> testCaseList = TestcaseMapper.selectAll();

        List<TestCaseDTO> testCaseDTOList = testCaseConverter.testCaseListForTestCaseDTOList(testCaseList);
        return testCaseDTOList;
    }

    @Override
    public int deleteTestCase(TestCaseDTO testCaseDTO) {
        //查找测试用例
        //1. testCaseDTO转换为testCase
        Testcase testCase = testCaseConverter.testCaseDTOForTestCase(testCaseDTO);
        //2. Mapper查找具体的testCase
        testCase = TestcaseMapper.selectOne(testCase);

        //如果测试用例存在则删除
        if(null != testCase){
            testCase.setStatus(0);
            int update = TestcaseMapper.updateByPrimaryKey(testCase);
            System.out.println("如果测试用例存在则删除:"+testCase);
            return update;
        }
        //否则返回0
        return 0;
    }


    @Override
    public int updateTestCase(TestCaseDTO testCaseDTO) {
        //更新testcase的status为0
        Testcase testCase = testCaseConverter.testCaseDTOForTestCase(testCaseDTO);
        Testcase getTestCase = new Testcase();
        getTestCase.setId(testCase.getId());
        getTestCase.setStatus(1);
        getTestCase = TestcaseMapper.selectOne(getTestCase);

        //更新内容
        getTestCase.setName(testCase.getName());
        getTestCase.setStep(testCase.getStep());
        getTestCase.setMethod(testCase.getMethod());
        getTestCase.setRemark(testCase.getRemark());
        getTestCase.setUpdateTime(new Date());
        System.out.println(getTestCase);
        int update = TestcaseMapper.updateByPrimaryKey(getTestCase);
        return update;
    }


}
